package com.ll.domain.classbase.event.consumer;

import com.ll.base.constant.LlConstants;
import com.ll.base.util.DateUtil;
import com.ll.base.util.RedisUtil;
import com.ll.domain.classbase.dto.CourseDTO;
import com.ll.domain.classbase.event.producer.AddCourserEvent;
import com.ll.domain.pk.dto.PkDTO;
import com.ll.domain.pk.event.producer.AddPkEvent;
import com.ll.domain.user.base.UserBaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * pk 记录添加
 * @author WuHen
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AddCourserEventToRobotListener implements ApplicationListener<AddCourserEvent> {

    private final RedisUtil redisUtil;


    @Async
    @Override
    public void onApplicationEvent(AddCourserEvent addCourserEvent) {
        CourseDTO pkDTO = addCourserEvent.getCourseDTO();
        log.info("接收到 课程信息添加事件 id ：{}", pkDTO.getId());
        //刷新机器人map
        Object mapObj = redisUtil.get(LlConstants.RedisContent.COURSE_LIST);
        Map<String, CourseDTO> map = new HashMap<>();
        if(mapObj != null){
            map = (Map<String, CourseDTO>) mapObj;
        }
        map.put(String.valueOf(map.size()) , pkDTO);
        redisUtil.set(LlConstants.RedisContent.COURSE_LIST, map, DateUtil.getDistanceTimeHour());
        log.info("接收到 课程信息添加事件 id ：{} 处理成功", pkDTO.getId());
    }
}
